// pages/components/mytree/mytree.js
const app = getApp()
// import {Quick} from '../../project/QuickInspection/QuickInspection'
Component({
  properties: {
    model: Object,
  },
  data: {
    // open: false,
    // isBranch: false,
    open: false, //是否展开
    isBranch: true, // 是否有子级
    parents: {},
    params: {},
    pic: '',
    isHidden: true, //隐藏模态框
    isHidden2: true, //隐藏模态框
    indexs: [],
    indexArr: [],
    indexId: '',
    mainId: '',
    indId: ''
  },

  methods: {
    toggle: function (e) {
      if (this.data.isBranch) {
        this.setData({
          open: !this.data.open,
        })
      }
    },
    tapItem: function (e) {
      var itemid = e.currentTarget.dataset.itemid;
      this.triggerEvent('tapitem', {
        itemid: itemid
      }, {
        bubbles: true,
        composed: true
      });
    },
    goChangeText: function (e) {
      // debugger;
      var itemid = e.currentTarget.dataset.itemid;
      var that = this;
      var ObjId = e.currentTarget.dataset.objid;
      var parentId = e.currentTarget.dataset.par;
      var type = e.currentTarget.dataset.type;
      var indexId = e.currentTarget.dataset.indexid;
      var pic = e.currentTarget.dataset.pic;
      console.log(ObjId);
      console.log(parentId);
      console.log(type);
      // debugger;
      if (type === "1") {
        wx.navigateTo({
          url: '/pages/project/object/object?subjectId= ' + ObjId + '&parentId=-1'
        })
      } else if (type === "2" || type === "3") {
        wx.navigateTo({
          url: '/pages/project/object/object?subjectId= ' + ObjId + '&parentId=' + itemid
        })
      } else if (type === "3.5") {
        wx.navigateTo({
          url: '/pages/project/object/object?subjectId= ' + ObjId + '&parentId=' + parentId
        })
      } else if (type === "2.5") {
        wx.navigateTo({
          url: '/pages/project/target/target?mainId=' + parentId + '&userId=' + app.login_s.id,
        })
      } else if (type === "4") {
        wx.navigateTo({
          url: '/pages/project/target/target?mainId=' + parentId + '&userId=' + app.login_s.id,
        })
      } else if (type === "5") {
        wx.navigateTo({
          url: '/pages/project/target/TarManage/TarManage?planId=' + ObjId + '&indexId=' + itemid
        })
      } else if (type === "20") {
        wx.navigateTo({
          url: '/pages/project/target/TarManage/OnSite/OnSite?indexId=' + indexId + '&eid=' + itemid + '&mainid=' + parentId + '&pic=' + pic,
        })
      } else if (type === "6" || type === "7" || type === "8") {
        wx.navigateTo({
          url: '/pages/project/target/TarManage/TarManage?planId=' + parentId + '&indexId=' + itemid
        })
      } else if (type === "30") {
        wx.navigateTo({
          url: '/pages/project/target/TarManage/TarData/TarData?planId=' + itemid,
        })
      } else if (type === "100" || type === "101" || type === "102" || type === "103") {
        this.setData({
          mainId: itemid
        })
        this.datailsShow()
      } else if (type === "44") {
        this.setData({
          mainId: parentId,
          indId: itemid,
        })
        this.showMd()
      }
    },

    // 新建指标弹窗
    datailsShow: function (e) {
      //弹出框展示套餐的具体商品，根据data-group得到数组对象，然后再渲染到界面上
      var self = this;
      // let groupList = e.currentTarget.dataset.group;
      //debugger
      this.setData({
        isHidden: false, //显示模态框，
        // groupArr: groupList,
        showView: false,
      })
    },
    hiddenMd: function () {
      this.setData({
        isHidden: true, //隐藏模态框
      })
    },
    // 弹窗
    showModal: function () {
      this.setData({
        showModal: true
      })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {},
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
      this.setData({
        showModal: false
      });
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function () {
      this.hiddenMd();
      this.setData({
        indexId: '',
      })
    },
    // 创建指标，点击确定接口
    goConfirm: function (e) {
      // debugger
      var that = this
      var indexId = this.data.indexId;
      for (var i = 0; i < this.data.indexs.length; i++) {
        if (indexId === this.data.indexs[i].name) {
          indexId = this.data.indexs[i].id;
        }
      }
      //var ObjId= e.currentTarget.dataset.objid;
      wx.request({
        //接口地址
        url: app.BASE_SERVER_URL + '/api/wx/addIndex',
        method: 'POST',
        data: {
          mainId: this.data.mainId,
          // mainId:this.data.params.mainId , 
          // collPerson: this.data.params.userId,
          collPerson: app.login_s.id,
          indexId: indexId,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success: (res) => { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
          console.log(res.data)
          if (res.data.code === '0') {
            this.hiddenMd();
            wx.showToast({
              title: "保存成功",
              icon: 'none',
              duration: 2000 //持续的时间                
            })
            //   刷新
            // this.triggerEvent("callSomeFun")
            this.triggerEvent("addSuccess")

          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000 //持续的时间                
            })
          }
          console.log(res.data)
        },
        fail: (err) => {
          console.log(err)
        }
      })
    },

    onShowTitle: function (e) {
      var that = this;
      that.setData({
        showView: (!that.data.showView)
      })
    },
    // 指标
    bindIndexList: function (e) {
      // debugger
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        indexId: this.data.indexArr[e.detail.value]
      })
    },

    // 新建测点弹窗
    showMd: function (e) {
      //弹出框展示套餐的具体商品，根据data-group得到数组对象，然后再渲染到界面上
      var self = this;
      // let groupList = e.currentTarget.dataset.group;
      //debugger
      this.setData({
        isHidden2: false, //显示模态框，
        // groupArr: groupList,
        showView: false,
      })
    },
    hiddenMd2: function () {
      this.setData({
        isHidden2: true //隐藏模态框
      })
    },

    // 弹窗
    showModal2: function () {
      this.setData({
        showModal2: true
      })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {},
    /**
     * 隐藏模态对话框
     */
    hideModal2: function () {
      this.setData({
        showModal2: false
      });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel2: function () {
      this.hideModal2();
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm2: function (e) {
      this.hiddenMd2();
      this.setData({
        name: '',
        code: '',
        enter_status: '',
        proposal: '',
        degree: '',
        number: '',
      })
    },

    // 添加测点，点击确定接口
    goConfirm2: function (e) {
      var status = this.data.enter_status;
      var statusObj = this.data.enterStatus;
      var ObjId = e.currentTarget.dataset.objid;
      // for(var i = 0;i<statusObj.length;i++){
      //   if(status === statusObj[i].label){
      //     status = statusObj[i].value;
      //   }
      // }
      wx.request({
        //接口地址
        url: app.BASE_SERVER_URL + '/api/wx/saveEnter',
        method: 'POST',
        data: {
          // planId: this.data.param.planId,
          planId: this.data.mainId,
          indexId: this.data.indId,
          name: this.data.name,
          code: this.data.code,
          // status:status,
          // dealWith:this.data.proposal,
          // assess:this.data.degree,
          remarks: this.data.remarks,
          userId: app.login_s.id,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success: (res) => { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
          if (res.data.code === '0') {
            this.hiddenMd2();
            wx.showToast({
              title: "保存成功",
              icon: 'none',
              duration: 2000 //持续的时间                
            })
            this.triggerEvent("addSuccess")
            // this.selectComponent('#add_mytree').bindBatch();
          } else {
            wx.showToast({
              title: "保存失败",
              icon: 'none',
              duration: 2000 //持续的时间                
            })
          }
        },
        fail: (err) => {
          console.log(err)
        }
      })
    },
    addSuccess: function () {
      this.triggerEvent('addSuccess')
    }
  },

  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      // this.setData({
      //   // parents:options,
      //   // pic:options.pic,
      //   params:options,
      // })
      wx.request({
        url: app.BASE_SERVER_URL + '/api/wx/getIndexList', //接口地址
        method: 'POST',
        data: {
          userId: app.login_s.id,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success: (res) => { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
          // console.log(res.data);
          if (res.data.code == '0') {
            var indexs = res.data.data.list;
            var indexArr = [];
            for (var i = 0; i < indexs.length; i++) {
              indexArr.push(indexs[i].name);
            }
            this.setData({
              indexs: indexs,
              indexArr: indexArr
            })
          }
        },
        fail: (err) => {
          console.log(err)
        }
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },


  showtree: function (e) {
    this.setData({
      isBranch: Boolean(this.data.model.childMenus && this.data.model.childMenus.length),
    });
    console.log(this.data.model)
    console.log(this.data.isBranch)
    // console.log(this.data);
  }


})

module.exports.Component = Component